package com.share.finance.risk.core.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.share.common.common.Page;
import com.share.common.enums.ScriptEnum;
import com.share.common.utils.GroovyUtil;
import com.share.finance.risk.core.mapper.VariateConfMapper;
import com.share.finance.risk.core.model.db.VariateConf;
import com.share.finance.risk.core.model.vo.VariablePageReq;
import com.share.finance.risk.core.service.IProductVariateService;
import com.share.finance.risk.core.service.IVariateConfService;
import groovy.lang.GroovyObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 风控变量默认值配置表  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-12-25
 */

@Slf4j
@Service
public class VariateConfServiceImpl extends ServiceImpl<VariateConfMapper, VariateConf> implements IVariateConfService {
    private Map<String, VariateConf> variateConfMap = new HashMap<>();
    private Map<String, GroovyObject> groovymap = new HashMap<>();
    @Autowired
    private VariateConfMapper variateConfMapper;

    @Autowired
    private IProductVariateService productVariateService;

    @Override
    public Object init() {
        Map<String, VariateConf> tmpVariateConfMap = new HashMap<>();
        Map<String, GroovyObject> tmpGroovymap = new HashMap<>();
        List<VariateConf> list = variateConfMapper.selectList(Wrappers.<VariateConf>query().ge("status", "1"));
        for (VariateConf po : list) {
            tmpVariateConfMap.put(po.getFieldId(), po);

            String groovyScript = po.getGroovyScript();
            if (StringUtils.isNotBlank(groovyScript) && ScriptEnum.GROOVY.id.equals(po.getSelectType())) {
                try {
                    GroovyObject groovyObject = GroovyUtil.createGroovyObject(groovyScript);
                    tmpGroovymap.put(po.getIntId(), groovyObject);//加入到缓存
                } catch (Exception e) {
                    log.error("groovy脚本初始化失败po={}", JSON.toJSONString(po), e);
                }
            }
        }
        groovymap = tmpGroovymap;
        variateConfMap = tmpVariateConfMap;
        return variateConfMap;
    }

    @Override
    public Object exeGroovyScript(String fieldId, String method, Object param) {
        try {
            return groovymap.get(fieldId).invokeMethod(method, param);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public VariateConf getVariateConf(String fieldId) {
        return variateConfMap.get(fieldId);
    }

    @Override
    public Page pageList(VariablePageReq req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        LambdaQueryWrapper<VariateConf> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(req.getFieldId())) {
            queryWrapper.like(VariateConf::getFieldId, req.getFieldId());
        }
        if (StringUtils.isNotBlank(req.getFieldCn())) {
            queryWrapper.like(VariateConf::getFieldCn, req.getFieldCn());
        }
        if (StringUtils.isNotBlank(req.getGroovyScript())) {
            queryWrapper.like(VariateConf::getGroovyScript, req.getGroovyScript());
        }
        if (StringUtils.isNotBlank(req.getIntId())) {
            queryWrapper.like(VariateConf::getIntId, req.getIntId());
        }
        if (StringUtils.isNotBlank(req.getJsonPath())) {
            queryWrapper.like(VariateConf::getJsonPath, req.getJsonPath());
        }
        if (StringUtils.isNotBlank(req.getVarType())) {
            queryWrapper.like(VariateConf::getVarType, req.getVarType());
        }
        if (StringUtils.isNotBlank(req.getStatus())) {
            queryWrapper.like(VariateConf::getStatus, req.getStatus());
        }
        List<VariateConf> list = variateConfMapper.selectList(queryWrapper);
        Page<VariateConf> page = new Page<>(list, new PageInfo<>(list).getTotal(), req);
        if (!CollectionUtils.isEmpty(list)) {
            for (VariateConf item : list) {
                item.setProductVariates(productVariateService.getByFieldId(item.getFieldId()));
            }
        }
        return page;
    }

    @Override
    public List<VariateConf> listAllVariable() {
        LambdaQueryWrapper<VariateConf> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(VariateConf::getStatus, "1");
        List<VariateConf> list = variateConfMapper.selectList(queryWrapper);
        return list;
    }
}
